Automatic Deployments: Create a CodePipeline
We'll cover the following
Defining our pipeline#
The pipeline comes in three stages:
- The Source stage pulls the latest code from GitHub.
- The Build stage builds the latest code with CodeBuild according to our
buildspec.yml
file. - The Deploy stage deploys the build artifacts from CodeBuild to the EC2 instances referenced in the deployment group, and starts the application according to our
appspec.yml
file.
Line #25: We don’t need to poll for changes because we’ll set up a webhook to trigger a deployment as soon as GitHub receives a change.
Now, let’s create the webhook that will trigger our pipeline as soon as a change is pushed to GitHub.
We also need to make some changes to our EC2 instance to get the CodeDeploy agent installed on it.
Line #12: The CodeDeploy agent requires ruby
.
Line #14: Downloads the CodeDeploy agent install script to /home/ec2-user/install
and makes it executable.
Line #18: Installs the CodeDeploy agent.
Line #29: See the next code listing for how to fill in this part.
Let’s update the UserData
section next. We need to remove the bits where we were downloading our application from GitHub because CodeDeploy will do that for us now.
And with all of that done, we can deploy our infrastructure updates. But first, we need to delete our stack from the CloudFormation console, because the changes we’ve made will not trigger CloudFormation to tear down our EC2 instance and start a new one. So, let’s delete our stack, and recreate it by running the deploy-infra.sh
script.
NOTE: Let’s run the code and also push all our infrastructure changes to our GitHub repository. Check out the
github.sh
file for that.
/
- deploy-infra.sh
At this point, our EC2 instance should be up and running with the CodeDeploy agent running on it. But the CodeDeploy agent doesn’t automatically deploy the application when it gets installed. For now, we can trigger the first deployment manually by hitting Release Change in the CodePipeline console. When we get to the Scaling section, we will have our EC2 instances deploy the application automatically as soon as they start.
As soon as the deployment completes, we should be able to see the “Hello World” message when we visit the URL we got after running deploy-infra.sh
.
We can now test our automatic deployments by making a change to the “Hello World” message in our application. Let’s change it to “Hello Cloud” and push the changes to GitHub.
NOTE: We have added
server.js
this time too. So before running the code, do change the message fromHello World\n
toHello Cloud\n
on Line #3. Then you cancurl
to see if it works.
/
- deploy-infra.sh
As soon as we push the changes to GitHub, we can watch the deployment progress in the CodePipeline console. As soon as the deployment reaches the Staging phase, we should see “Hello Cloud” when we refresh the URL.
Our application is now getting updated automatically as soon as a change gets pushed to GitHub. And since we’re now using GitHub access tokens, we can also mark our repository as private.
In order to get a pictorial view of our developed cloudformation stack so far, below is the design view which shows the resources we created and their relationships.
In the next lesson, we will run our application on more than one EC2 instance.